<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleAnalyze">触发分析</el-button>
        </el-form-item>
      </el-form>
      <WmsTable v-loading="loading" :data="wmsInventoryAnalysisList" @selection-change="handleSelectionChange">
        <el-table-column v-if="columns[4].visible" align="center" label="id" prop="id"></el-table-column>
        <el-table-column v-if="columns[4].visible" align="center" label="message" prop="message"></el-table-column>
        <el-table-column v-if="columns[5].visible" align="center" label="创建时间" prop="createTime"></el-table-column>
      </WmsTable>
      <pagination v-show="total&gt;0" :limit.sync="queryParams.size" :page.sync="queryParams.page" :total="total"
                  @pagination="getList"></pagination>
    </div>
  </template>

  <script>
  import { listWmsInventoryHistory, getWmsInventoryHistory, delWmsInventoryHistory, addWmsInventoryHistory, updateWmsInventoryHistory, exportWmsInventoryHistory } from "@/api/wms/inventoryHistory";
  import { mapGetters } from 'vuex'
  import {queryAllAdvice} from '@/api/wms/warehouse'
  import {executeAnalysis} from '@/api/wms/warehouse'
  export default {
    name: "WmsInventoryAnalysis",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 库存记录表格数据
        wmsInventoryAnalysisList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          page: 1,
          size: 10,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        },
        columns: [
              { key: 1, label: "操作id（出库、入库、库存移动表单id）", visible:  true  },
              { key: 2, label: "操作类型", visible:  true  },
              { key: 3, label: "物料ID", visible:  true  },
              { key: 4, label: "货架id", visible:  true  },
              { key: 5, label: "库存变化", visible:  true  },
              { key: 6, label: "备注", visible:  true  },
                               ],
      };
    },
    computed: {
      ...mapGetters(['opTypeMap'])
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询库存记录列表 */
      getList() {
        this.loading = true;
        const {page, size} = this.queryParams;
       this.queryParams.page +=1;
        queryAllAdvice(this.queryParams).then(response => {
          const { content, totalElements } = response
          this.wmsInventoryAnalysisList = content;
          this.total = totalElements;
          this.loading = false;
        });
      },
      analyze() {
        executeAnalysis().then(response => {
          if (response.status == 200) {
            this.$modal.msgSuccess("调用分析任务成功，请等待一段时间");
          } else {
            this.$modal.msgError("调用失败，请稍后再试");
          }
        })
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          formId: null,
          formType: null,
          itemId: null,
          rackId: null,
          quantity: null,
          remark: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.queryParams.warehouseArr = null
        this.handleQuery();
      },
      handleAnalyze() {
        this.analyze();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加库存记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getWmsInventoryHistory(id).then(response => {
          this.form = response;
          this.open = true;
          this.title = "修改库存记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateWmsInventoryHistory(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addWmsInventoryHistory(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除库存记录编号为"' + ids + '"的数据项？').then(function() {
          return delWmsInventoryHistory(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$modal.confirm('是否确认导出所有库存记录数据项？').then(() => {
          this.exportLoading = true;
          return exportWmsInventoryHistory(queryParams);
        }).then(response => {
          this.$download.download(response);
          this.exportLoading = false;
        }).catch(() => {});
      }
    }
  };
  </script>
